﻿// lindexi
// 10:46

using ModelClassLibrary;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace ArticleandSiteData
{
    public static class BmobWebsiteData
    {
        public static async Task<List<T>> GetAllwebsitesinfo<T>(string str)
        {
            List<T> resualist = null;
            string resultstr = await RequestHelper.GetHttpstringrequest(str);
            resualist = (List<T>) JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<System.Net.Http.HttpResponseMessage> Putjson<HttpResponseMessage>(string str,
            string jsonstr)
        {
            var resualist = await RequestHelper.PutHttpstringrequest(str, jsonstr);

            return resualist;
        }

        public static async Task<T> GetAllwebsitesinfoone<T>(string str)
        {
            string resultstr = await RequestHelper.GetHttpstringrequest(str);
            var resualist = (T) JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<T> GetOneuserwebsitesinfoone<T>(string str)
        {
            string resultstr = await RequestHelper.GetHttpstringrequest(str);
            var resualist = (T) JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<System.Net.Http.HttpResponseMessage> AddoneWebsite<HttpResponseMessage>(string str,
            string jsonstr)
        {
            var resualist = await RequestHelper.PostHttpstringrequest(str, jsonstr);
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<System.Net.Http.HttpResponseMessage> Modeify<HttpResponseMessage>(string str,
            string jsonstr)
        {
            var resualist = await RequestHelper.PutHttpstringrequest(str, jsonstr);
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<System.Net.Http.HttpResponseMessage> AddOne<HttpResponseMessage>(string str,
            string jsonstr)
        {
            var resualist = await RequestHelper.PostHttpstringrequest(str, jsonstr);
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<System.Net.Http.HttpResponseMessage> AddoneWebsiteBystringcontnet<HttpResponseMessage>(
            string str, StringContent strcontent)
        {
            var resualist = await RequestHelper.PostHttpstringrequestbystringcontent(str, strcontent);
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<string> Login(string str, FormUrlEncodedContent strcontent)
        {
            var resualist = await RequestHelper.GetHttpstringrequest(str, strcontent);
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }

        public static async Task<T> Loginwithstriing<T>(string str)
        {
            var resualist = await RequestHelper.GetHttpstringrequest(str);
            var oneT = (T) JsonConvert.DeserializeObject(resualist, typeof(T));
            return oneT;
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            //  return resualist;
        }

        public static async Task<List<Info>> Getrecommendsites(string str)
        {
            using (var resualist = await RequestHelper.GetHttpstreamrequest(str))
            {
                XElement p = XElement.Load(resualist);
                List<Info> items = new List<Info>();
                XName xitemname = XName.Get("item");
                IEnumerable<XElement> itemnodes = p.Descendants(xitemname).ToList<XElement>();
                foreach (var b in itemnodes)
                {
                    XName xurl = XName.Get("url");
                    XName xname = XName.Get("text");
                    XName xdesname = XName.Get("des");
                    items.Add(
                        new Info()
                        {
                            des = b.Attributes(xdesname).First().Value,
                            text = b.Attributes(xname).First().Value,
                            dataurl = b.Descendants(xurl).First().Value
                        });
                }
                return items;
            }

            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            //  return resualist;
        }

        public static async Task<bool> QueryObect(string requesturl)
        {
            var res = await RequestHelper.QueryObject(requesturl);
            return res.StatusCode == HttpStatusCode.OK;
        }

        public static async Task<Favratesites> Getfavoratesites(string str)
        {
            var resualist = await RequestHelper.GetHttpstringrequest(str);
            var oneT = (Favratesites) JsonConvert.DeserializeObject(resualist, typeof(Favratesites));

            return oneT;
        }

        public static async Task<T> GetLists<T>(string str)
        {
            var resualist = await RequestHelper.GetHttpstringrequest(str);
            var oneT = (T) JsonConvert.DeserializeObject(resualist, typeof(T));

            return oneT;
        }

        /// <summary>
        ///     反序列化
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string xml)
        {
            try
            {
                using (StringReader stream = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(type);
                    return xmldes.Deserialize(stream);
                }
            }
            catch (Exception )
            {
                return null;
            }
        }

        private static List<Info> _items = new List<Info>();
    }
}